package com.loopj.android.http.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import com.loopj.android.http.RequestParams;
import com.loopj.android.http.util.Response.ErrorListener;
import com.loopj.android.http.util.Response.Listener;

public abstract class MoccaRequest<T> {
	public String requestType = "POST";
	public String url;
	public RequestParams params;
	public Listener<T> listener;
	public ErrorListener errorListener;
    private final Map<String, String> clientHeaderMap;

	public MoccaRequest(String url, Listener<T> listener, ErrorListener errorListener) {
		super();
		this.url = url;
		this.requestType = "POST";
		this.listener = listener;
		this.errorListener = errorListener;
		params = new RequestParams();
		clientHeaderMap = new HashMap<String, String>();
	}

	public MoccaRequest(String url, String requestType, Listener<T> listener, ErrorListener errorListener) {
		super();
		this.url = url;
		this.requestType = requestType;
		this.listener = listener;
		this.errorListener = errorListener;
		params = new RequestParams();
		clientHeaderMap = new HashMap<String, String>();
	}

	public RequestParams getRequestParams() {
		return null;
	}

	public Header[] getHeaders() {
		Header[] headers = new Header[clientHeaderMap.size()];
		int i = 0;
		for (String header : clientHeaderMap.keySet()) {
			headers[i] = new BasicHeader(header, clientHeaderMap.get(header));
			i++;
        }
		return headers;
	}
	
	public void addHeader(String key, String value) {
		clientHeaderMap.put(key, value);
	}

	public void addParams(String key, Object value) {
		params.add(key, value.toString());
	}
	
	public abstract T parseNetworkResponse(String response);

	public void parse(int statusCode, Header[] headers, byte[] responseBody) {
		listener.onResponse(parseNetworkResponse(new String(responseBody)));
	}
	
}
